package cn.huihuo.weibo.service;

import cn.huihuo.weibo.bean.comment.CommentBean;
import cn.huihuo.weibo.bean.comment.CommentPage;
import cn.huihuo.weibo.bean.comment.CommonResult;
import cn.huihuo.weibo.module.Comment;
import cn.huihuo.weibo.module.Hot;
import cn.huihuo.weibo.module.Message;
import cn.huihuo.weibo.module.ProdComment;
import cn.huihuo.weibo.utils.TimeUtils;
import cn.jfinalbbs.index.IndexAdminController;
import cn.jfinalbbs.user.User;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengjunbo on 2015/10/9.
 */
public class CommentService {
    public static CommentService commentService = new CommentService();

    public CommentPage getCommentPage(int pageNumber, int pageSize, String prodCommentId){
        Page<Comment> product = Comment.comment.findByProdCommentId(pageNumber, pageSize, prodCommentId);
        List<Comment> comments = product.getList();

        CommentPage commentPage = new CommentPage();
        commentPage.setTotalSize(String.valueOf(product.getTotalPage()));
        commentPage.setCode("200");
        commentPage.setDescription("success");
        commentPage.setCommentData(new ArrayList<>());
        for(Comment comment:comments){
            Integer commCommId = comment.getInt("commCommId");
            String replayUserId = null;
            String replayNickname = null;
            String replayUserUrl = null;
            if(commCommId!=null){
                Comment commById = Comment.comment.findByCommId(String.valueOf(commCommId));
                if(commById!=null){
                    replayUserId = commById.getStr("userId");
                    replayNickname = commById.getStr("nickname");
                    replayUserUrl = "/user/" +replayUserId;
                }


            }
            CommentBean commentBean = new CommentBean();
            commentBean.setCid(String.valueOf(comment.getInt("commId")));
            commentBean.setCommentContent(comment.getStr("COMMENT"));
            commentBean.setUserId(comment.getStr("userId"));
            commentBean.setCommentUserUrl("/user/"+comment.getStr("userId"));
            commentBean.setCommentUserName(comment.getStr("nickname"));
            commentBean.setCommentAvatar(comment.getStr("avatar"));

            commentBean.setUgcId(String.valueOf(comment.getInt("ugc_id")));
            commentBean.setReplayUserId(replayUserId);
            commentBean.setCommentReplyUserName(replayNickname);
            commentBean.setCommentReplyUserUrl(replayUserUrl);
            commentBean.setCommentPublishTime(TimeUtils.getTime(comment.getTimestamp("commentTime").getTime()));
            commentPage.getCommentData().add(commentBean);

        }
        String pageCode = getPageCode(product.getPageNumber(),product.getTotalPage());
        commentPage.setCommentPageCode(pageCode);
        return commentPage;
    }

    private String getPageCode(int pageNumber, int totalPage) {
        String pre = "<a class=\"pre\">上一页</a>";
        String next = "<a href=\"javascript:;\" class=\"next\">下一页</a>";
        String result = "";
        if(totalPage==0){
            return result;
        }
        if(!(pageNumber == 1)){
            result = result+pre;
        }

        if(totalPage<=6){
            for(int i = 1 ; i <= totalPage ; i++){
                if(i==pageNumber){
                    result = result+"<a class=\"c\">"+i+"</a>";
                } else {
                    result = result+"<a href=\"javascript:;\">"+i+"</a>";
                }
            }
        } else {
            for(int i = 1 ; i <= totalPage ; i++){
                if((pageNumber-3)<i && i<(pageNumber+3)){
                    if(i==pageNumber){
                        result = result+"<a class=\"c\">"+i+"</a>";
                    } else {
                        result = result+"<a href=\"javascript:;\">"+i+"</a>";
                    }
                }
            }
        }

        if(totalPage!=pageNumber){
            result=result+next;
        }


        return result;
    }

    public CommonResult saveComment(String ugcId,
                                    String commentContent,
                                    String cid,
                                    String userId,
                                    String replayUserId){//回复者的id，需要被回复者的id
        Comment comment = new Comment();
        boolean save  = comment.set("ugc_id", ugcId)
                    .set("comment", commentContent)
                    .set("user_id", userId)
                    .set("comment_id",cid)
                    .save();

        ProdComment prodComment = ProdComment.prodComment.findById(ugcId);
        ParticipationService.service.updateParticipation(prodComment.getInt("prod_id"));
        ParticipationService.service.tryPutParticipation(userId,prodComment.getInt("prod_id"));
        CommonResult commonResult = new CommonResult();
        if(save){
            commonResult.setCid(String.valueOf(comment.getInt("id")));
            commonResult.setCommentPublishTime("刚刚");
            commonResult.setCommentContent(commentContent);


            //对消息进行通知根据cid是否有来决定是什么类型
            Message message = new Message();
            boolean save1 = message.set("type", cid == null ? "prodComm" : "comm")
                    .set("user_id", replayUserId)//通知给被回复者
                    .set("reply_id", comment.getInt("id"))
                    .save();

            HotService.service.ugcHot(Integer.valueOf((ugcId)));
            return commonResult;
        } else {
           throw new RuntimeException("保存失败！");
        }
    }
}
